<?php
/**
 * CVRanking navigation
 */

/**
 *
 * @todo : Allow import/export of objects through pageshell (e.g. XML).
 * @todo : Add buttons "import" and "export".
 */
$CONFIG;

$page_owner = $vars ['owner'];

$object = array (
		'owner' => $page_owner 
);

$tab = get_input ( 'tab' );

$tabs = array ();

// Add projects/groups tab.
if (! $tab) {
	$tab = 'personal';
}
$tabs [] = array (
		'title' => elgg_echo ( 'resume:label:personal' ),
		'url' => "$url" . '?tab=personal',
		'selected' => ($tab == 'personal') 
);

if (elgg_get_plugin_setting ( 'skill' ) == 'yes') {
	if (! $tab)
		$tab = 'skill';
	$tabs [] = array (
			'title' => elgg_echo ( 'resume:label:skill' ),
			'url' => "$url" . '?tab=skill',
			'selected' => ($tab == 'skill') 
	);
}

// --------------- Modified by Toan Nguyen. -----------------//

// Add projects/groups tab.
if (! $tab) {
	$tab = 'awards';
}
$tabs [] = array (
		'title' => elgg_echo ( 'resume:label:awards' ),
		'url' => "$url" . '?tab=awards',
		'selected' => ($tab == 'awards') 
);

// Add projects/groups tab.
if (! $tab) {
	$tab = 'projectgroups';
}
$tabs [] = array (
		'title' => elgg_echo ( 'resume:label:projects_groups' ),
		'url' => "$url" . '?tab=projectgroups',
		'selected' => ($tab == 'projectgroups') 
);

// ------------------------------------------------------------//

$params = array (
		'tabs' => $tabs 
);

echo elgg_view ( 'navigation/tabs', $params );
switch ($tab) {
	case 'resume' :
		echo elgg_view ( 'resume/tab/resume', $object );
		break;
	
	case 'skill' :
		$skillview = elgg_list_entities ( array (
				'types' => 'object',
				'subtypes' => 'skill',
				'container_guid' => $page_owner->guid,
				'limit' => 0,
				'full_view' => false 
		) );
		if (strlen ( $skillview ) < 70) {
			echo "<div style='margin-left:20px;'>Chưa có dữ liệu</div>";
		} else {
			echo $skillview;
		}
		
		break;
	case 'projectgroups' :
		$dbprefix = elgg_get_config ( 'dbprefix' );
		
		$projectgroups = elgg_list_entities_from_relationship ( array (
				'relationship' => "showedproject",
				'relationship_guid' => elgg_get_logged_in_user_guid (),
				'type' => "object",
				'subtype' => "project",
				'student_project' => true,
				'limit' => 10,
				'pagination' => true 
		) );
		
		if (strlen ( $projectgroups ) < 70) {
			$projectgroups = elgg_list_entities_from_relationship ( array (
					'relationship' => "doneproject",
					'relationship_guid' => elgg_get_logged_in_user_guid (),
					'type' => "object",
					'subtype' => "project",
					'student_project' => true,
					'limit' => 10,
					'pagination' => true 
			) );
		}
		if (strlen ( $projectgroups ) < 70) {
			echo "<div style='margin-left:20px;'>Chưa tham gia bất kì đồ án nào</div>";
		} else {
			echo $projectgroups;
		}
		break;
	case 'awards' :
// 		$awards = elgg_get_entities ( array (
// 				'type' => "object",
// 				'subtype' => 'award',
// 				'container_guid' => $page_owner->guid,
// 				'limit' => 0,
// 				'full_view' => false
// 		) );
// 		foreach ($awards as $award){
// 			$award->delete(true);
// 		}
		
		$awardsview = elgg_list_entities ( array (
				'type' => "object",
				'subtype' => 'award',
				'container_guid' => $page_owner->guid,
				'limit' => 0,
				'full_view' => false 
		) );
		if (strlen ( $awardsview ) < 70) {
			echo "<div style='margin-left:20px;'>Chưa có dữ liệu</div>";
		} else {
			echo $awardsview;
		}
		break;
	case 'personal' :
		$owner_page = elgg_get_page_owner_entity ();
		
		
// 		$personals = elgg_get_entities(array(
// 			'type' => "object",
// 			'subtype' => "personal",
// 			'owner_guid' => $owner_page->guid,
// 		));
// 		foreach ($personals as $personal){
// 			$personal->delete(true);
// 		}
		
// 		$count = count($personals);
// 		var_dump($count);
		
		$personalView = elgg_list_entities ( array (
				'type' => 'object',
				'subtype' => 'personal',
				'owner_guid' => $owner_page->guid,
				'limit' => 1,
				'full_view' => true,
				'pagination' => false,
		) );
		
// 		var_dump($owner_page->guid);
		
		if (strlen ( $personalView ) < 70) {
			
			$namelabel = elgg_echo ( 'resume:personal:name' );
			$doblabel = elgg_echo ( 'resume:personal:dob');
			$emaillabel = elgg_echo ( 'resume:personal:email' );
			$genderlabel = elgg_echo ( 'resume:personal:gender' );
			$phonelabel = elgg_echo ( 'resume:personal:phone' );
			$addresslabel = elgg_echo ( 'resume:personal:address' );
			$facultylabel = elgg_echo('resume:personal:faculty');
			$moredetaillabel = elgg_echo ( 'resume:personal:moredetail' );
			
			$nameview = $owner_page->username;
			$dobview = "--";
			$genderview = "--";
			$emailview = $owner_page->email;
			$phoneview = "--";
			$addressview = "--";
			$facultyview = "--";
			$moredetailview = "--";
			
			$personalView .= "<div id='wrapcontent' >";
			$personalView .= "<table id='personal_table' style='border: solid 0px #ffffff; width:100%;'>";
			$personalView .= "<tbody>
		<tr>
			<td> $namelabel</td>
			<td> $nameview </td>	
		</tr>
		<tr>
			<td>$genderlabel</td>
			<td> $genderview</td>
		</tr>
		<tr>
			<td> $emaillabel</td>
			<td>$emailview </td>
		</tr>
		<tr>
			<td>$doblabel</td>
			<td>$dobview</td>
		</tr>
		<tr>
			<td>$phonelabel</td>
			<td>$phoneview</td>
		</tr>
		<tr>
			<td>$addresslabel</td>
			<td>$addressview</td>
		</tr>
		<tr>
			<td>$facultylabel</td>
			<td>$facultyview</td>
		</tr>
		<tr>
			<td>$moredetaillabel</td>
			<td>$moredetailview</td>
		</tr>
	</tbody>
</table>
</div>";
		}
		
		echo $personalView;
		
		if (($owner_page instanceof ElggUser) && ($owner_page->getGUID () == elgg_get_logged_in_user_guid ())) {
			if (! isset ( $personalView ) || $personalView == '') {
				$linkEditPersonal = $CONFIG->wwwroot . 'resumes/' . $tab;
			} else {
				$linkEditPersonal = $CONFIG->wwwroot . 'resumes/' . $tab . '/' . $page_owner->guid;
			}
			echo '<a class="elgg-button elgg-button-action" style="float:right;margin-bottom:5px" href="' . $linkEditPersonal . '">' . elgg_echo ( 'resume:personal:edit' ) . '</a>';
		}
		break;
	default :
		break;
}
